<template>
	<view class="page">
		<!-- 		<live-player  id="live-video"  src="http://42.193.153.242/live?port=1935&app=live&stream=test" autoplay @statechange="statechange" @error="error"
			style="width: 300px; height: 225px;" /> -->

		<!-- 		<video id="myVideo"
			src="http://42.193.153.242/live?port=1935&app=live&stream=test"
			@error="videoErrorCallback" enable-danmu danmu-btn controls></video> -->

		<!-- 搜索栏 ——start-->
<!-- 		<view class='search '>
			<u-input placeholder="梅州市金绿集团羊岭村基地" class="search_input">
				<template slot="suffix">
					<u-icon name="search" color="#23b8ff" size="28"></u-icon>
				</template>
			</u-input>
		</view> -->
		<!-- 搜索栏 ——end-->

		<!-- 基地card ——start -->
		<view class="content" style="height: 500rpx;" @click="interBase(item)" v-for="(item,index) in baseList" :key="index">
			<view class="title">{{item.name}}</view>
			<view class="charts-box">
				<qiun-data-charts type="column" :chartData="item.chartData" />
			</view>
		</view>
		<!-- 		<view class="content" style="height: 500rpx;" @click="interBase">
			<view class="title">梅州市金绿集团羊岭村基地</view>
			<view class="charts-box">
				<qiun-data-charts type="column" :chartData="chartData" />
			</view>
		</view>
		<view class="content" style="height: 500rpx;" @click="interBase">
			<view class="title">梅州市金绿集团羊岭村基地</view>
			<view class="charts-box">
				<qiun-data-charts type="column" :chartData="chartData" />
			</view>
		</view> -->
		<!-- 基地card——end -->
	</view>
</template>

<script>
	var baseListTemp = [];
	import uCharts from "../../common/u-charts.js";
	import {
		myRequest
	} from "../../util/api.js"
	export default {
		name: 'base',
		meta: {
			title: "基地页面",
		},
		data() {
			return {
				// chartData: {
				// 	categories: ["肉鸽", "光蛋", "鸽粪", "残鸽"],
				// 	series: [{
				// 		name: "上月产出比例",
				// 		data: [35, 8, 25, 37]
				// 	}]
				// },
				haveData: false,
				header: {
					token: ''
				},
				baseList: [] //基地列表
			}
		},
		onLoad(options) {
			wx.cloud.init()
			// console.log('再执行一次')
			
			
			if (getApp().globalData.isLogin) {
				
			} else {
				this.getIp(this.quickLogin);
			}
			getApp().watch('isLogin',() => {
				if (getApp().globalData.isLogin) {
					this.getUserInfo()
					this.getBaseList()
				}
			},{
				immediate: true
			})
		},
		onShow() {

		},
		methods: {
			statechange(e) {
				// console.log('live-player code:', e.detail.code)
			},
			error(e) {
				// console.error('live-player error:', e.detail.errMsg)
			},
			//用户ip
			getIp(callback) {
				// wx.request({
				//     url: 'http://ip-api.com/json',
				//     success:function(e){
				// 		// console.log('用户ip',e.data.query);

				// 		// console.log('用户ip',getApp().globalData.xip)
				//     }
				// });  
				wx.request({
					url: 'https://pv.sohu.com/cityjson?ie=utf-8',
					success: async function(e) {
						// console.log(e.data,'ip')
						let ip;
						let index = e.data.indexOf('=')
						// console.log(e.data.substring(index + 1))
						let temp = e.data.substring(index + 1);
						temp = temp.substring(0, temp.indexOf(';'))
						// console.log(JSON.parse(temp))
						ip = JSON.parse(temp).cip;
						// console.log(ip)
						getApp().globalData.xip = ip;
						// 回调
						callback && callback()
					},
				})
			},
			async quickLogin() {
				let that = this;
				// console.log('微信快捷登录');
				wx.login({
					success: async (res) => {
						// console.log('登录结果', res.code)
						if (res.code) {
							let weChatLoginDto = {
								code: ''
							}
							// console.log(getApp().globalData.xip)
							weChatLoginDto.code = res.code;
							// console.log(loginBody)
							uni.setStorageSync('code', res.code)
							wx.cloud.callContainer({
								config: {
									env: "prod-9gaetadn0414eb27"
								},
								path: "/authority/user/wechat/login",
								header: {
									"X-WX-SERVICE": "springboot-authority",
									"content-type": "application/json",
									xip: getApp().globalData.xip
								},
								method: "POST",
								data: weChatLoginDto,
								success(res) {
									// console.log('成功', res);


									//快捷登录失败，跳转到登录页面
									if (res.data.statusCode === 20001) {
										 uni.redirectTo({
											url: '../login/login'
										});
									} else {
										let token = res.header.token;
										uni.setStorageSync('token', token);
										getApp().globalData.isLogin = true;
										
									}
								},
								fail(error) {
									// console.log('失败', error)
								}
							})


						} else {
							// console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			getUserInfo() {
				wx.cloud.callContainer({
					config: {
						env: "prod-9gaetadn0414eb27"
					},
					path: "/authority/user",
					header: {
						"X-WX-SERVICE": "springboot-authority",
						"content-type": "application/json",
						xip: getApp().globalData.xip,
						token: uni.getStorageSync('token')
					},
					method: "GET",
					success(res) {
						if (res.data.statusCode === 20000) {
							
							let userInfo = res.data.data;
							uni.setStorageSync('userInfo',userInfo)
							
						} else {
							uni.showToast({
								title: '获取信息失败',
								duration: 2000,
								icon: 'error'
							});
						}
					},
					fail(error) {
						// console.log('获取用户信息失败', error)
					}
				})
			},
			interBase(item) {
				// console.log('执行');
				uni.setStorageSync('id',item.id);
				uni.setStorageSync('baseName',item.name);
				uni.navigateTo({
					url: '../../page_base/base/base',
				});
			},
			videoErrorCallback(errro) {
				// console.log('错误', errro)
			},
			async getBaseList() {
				let option = {
					url: `http://39.99.247.58:9802/breed/Base/list/1/100`,
					header: {
						token: ''
					}
				}
				option.header.token = uni.getStorageSync('token')
				let res = await myRequest(option);
				// console.log('基地列表', res)
				if (res.data.statusCode === 20000) {
					// console.log('获取数据成功');
					let temp = res.data.data.records;


					temp.forEach((item, index) => {
						//处理数据
						let object = {
							name: '',
							id: '',
							chartData: {
								categories: ["异常鸽", "残次品", "鸽蛋", "鸽粪", "肉鸽"],
								series: [{
									name: '上月产出比例',
									data: []
								}]
							}

						}
						object.name = item.name;
						object.id = item.id;
						baseListTemp.push(object);
					})


					this.getAllProductionType()
				}

			},

			async getAllProductionType() {
				let option = {
					url: '/breed/shipmentOutBill/getAllTypeAmountOfMonth',
					data: null,
					header: {
						token: ''
					}
				}
				option.header.token = uni.getStorageSync('token');

				let d = new Date();
				let year = d.getFullYear();
				let mounth = d.getMonth() + 1;
				if (mounth === 1) {
					//若是1月的则要查看去年12月的数据
					year--;
					mounth = 12;
				}

				baseListTemp.forEach(async (item, index) => {

					if (index == 0) {
						item.chartData = {
							categories: ["肉鸽", "光蛋", "鸽粪", "残鸽"],
							series: [{
								name: "上月产出比例",
								data: [35, 8, 25, 30]
							}]
						}
					}
					if (index == 1) {
						item.chartData = {
							categories: ["肉鸽", "光蛋", "鸽粪", "残鸽"],
							series: [{
								name: "上月产出比例",
								data: [2, 28, 10, 37]
							}]
						}
					}
					
					if (index == 2) {
						item.chartData = {
							categories: ["肉鸽", "光蛋", "鸽粪", "残鸽"],
							series: [{
								name: "上月产出比例",
								data: [35, 8, 25, 37]
							}]
						}
					}
					if (index == 3) {
						item.chartData = {
							categories: ["肉鸽", "光蛋", "鸽粪", "残鸽"],
							series: [{
								name: "上月产出比例",
								data: [2, 28, 10, 37]
							}]
						}
					}
					//实际上，但是现在没数据，所以先注释掉
					// let body = {
					// 	baseId: item.id,
					// 	pageNum: 1,
					// 	pageSize: 12,
					// 	year: year
					// }

					// option.data = body;

					// let res = await myRequest(option);
					// let chartData = null
					// if (mounth === 1) {

					// 	//此时在上面已经减去了
					// 	chartData = res.data.data.records[mounth - 1];
					// 	console.log(chartData)
					// } else {
					// 	chartData = res.data.data.records[mounth - 2];
					// }
					// for (let i in chartData) {
					// 	if (i === "disabledDove") {
					// 		item.chartData.series[0].data[0] = chartData[i];
					// 		continue;
					// 	} else if (i === "abnormalDove") {
					// 		item.chartData.series[0].data[1] = chartData[i];
					// 		continue;
					//  } else if (i === "doveEggs") {
					// 		item.chartData.series[0].data[2] = chartData[i];
					// 		continue;
					// 	} else if (i === "doveShit") {
					// 		item.chartData.series[0].data[3] = chartData[i];
					// 		continue;
					// 	} else {
					// 		item.chartData.series[0].data[4] = chartData[i];
					// 	}
					// }

				})
				console.log(baseListTemp[0].chartData, 111);
				// this.
				this.baseList = baseListTemp
				//参数

			}
		}
	}
</script>

<style lang="scss" scoped>
	* {
		padding: 0;
		margin: 0;
		box-sizing: border-box;
	}

	.page {
		background-color: #F8F8F8;
		height: 100%;

		//搜索栏
		.search {
			height: 55px;
			width: 100%;
			padding: 10rpx 30rpx 10rpx 30rpx;
			margin-top: 0;

			/deep/ .u-input {
				// flex: 7;
				border: 3rpx solid #23b8ff !important;
				height: 80rpx;
				padding-left: 20rpx;
				border-radius: 35rpx;
				font-size: 30rpx;
				background-color: #fff;
			}

			.searchIcon {
				// flex: 1;
				height: 38px;
				line-height: 38px;
				padding-left: 5rpx;
				font-size: 60rpx;
				margin-top: 5rpx;
				margin-left: 10rpx;
			}
		}

		.content {
			display: flex;
			flex-flow: column wrap;
			margin: 30rpx 20rpx 0 20rpx;
			padding: 10rpx;
			background-color: #FFFFFF;
			box-shadow: 0 0 8rpx rgba(0, 0.5, 0.5, 0.2);

			.title {
				width: 100%;
				height: 100rpx;
				padding: 20rpx;
				text-align: center;
				font-size: 40rpx;
				font-weight: 700;
				border-bottom: 2rpx solid #D9D9D9;
				overflow: hidden;
				text-overflow:ellipsis;
				white-space: nowrap;
			}

			.charts-box {
				margin: 0 auto;
				width: 650rpx;
				height: 380rpx;
			}
		}
	}
</style>
